package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.atguigu.common.valid.AddGroup;
import com.atguigu.common.valid.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;


/**
 * 品牌
 *
 * @author manjusri
 * @email sunlightcs@gmail.com
 * @date 2020-09-09 01:10:17
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
/*    @RequestMapping("/info/{brandId}")
   // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }*/

    /**
     * 保存
     */
 //   @RequestMapping("/save")
   // @RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand){
//        if (result.hasErrors()){
//            Map<String,String> map = new HashMap<>();
//            result.getFieldErrors().forEach((item)->{
//                String message = item.getDefaultMessage();
//                String field = item.getField();
//                map.put(field,message);
//            });
//            return R.error(400,"提交的数据不合法").put("data",map);
//        }
//        else{
            brandService.save(brand);
            return R.ok();

    }

    /**
     * 修改
     */
    @Transactional
//    @RequestMapping("/update")
  //  @RequiresPermissions("product:brand:update")
    public R update(@RequestBody @Validated({UpdateGroup.class}) BrandEntity brand){
		brandService.updateDetail(brand);
        return R.ok();
    }







    @RequestMapping("/update/status")
    //  @RequiresPermissions("product:brand:update")
    public R updateStatus(@RequestBody BrandEntity brand){
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update1(@RequestBody BrandEntity brandEntity ){
        brandService.updateByIds(brandEntity);
        return R.ok();
    }

    @RequestMapping("/info/{brandId}")
    public R lookList(@PathVariable("brandId") Long id){
        BrandEntity brand = brandService.lookList(id);
        return R.ok().put("brandEntity",brand);
    }

    /**
     * 新增
     *
     */
  //  @RequestMapping("/save")
  /*  public R saveList(@RequestBody BrandEntity brandEntity){


        brandService.saveList(brandEntity);
        return R.ok();
    }  */

    @RequestMapping("/save")
    public R saveList(HttpServletRequest request){
        String name = request.getParameter("name");
        BrandEntity brandEntity1 = new BrandEntity();
        brandEntity1.setName(name);
        brandService.saveList(brandEntity1);
        return R.ok();
    }


    @RequestMapping("/info1")
    // @RequiresPermissions("product:brand:delete")
    public R select(){


        List<BrandEntity> list = brandService.selectAll();

        return R.ok().put("list",list);
    }
}
